import { CCObject, Node, System, director, Director, UIRenderer,Color } from "cc"
declare module "cc" {
  interface Renderable2D {
    colorR: number
    colorG: number
    colorB: number
    colorA: number
  }

  interface Node {
    alpha:number
    positionX: number
    positionY: number
    positionZ: number
    worldPositionX: number
    worldPositionY: number
    worldPositionZ: number
    eulerAngleX: number
    eulerAngleY: number
    eulerAngleZ: number
    scaleX: number
    scaleY: number
    scaleZ: number
    scaleXYZ: number
    color:Color
  }
}

Object.defineProperties(UIRenderer.prototype, {
  colorR: {
    get: function () {
      return this.color.r
    },
    set: function (v) {
      if (this.color.r == v) return
      this.color = new Color(v,this.color.g,this.color.b,this.color.a)
    },
    enumerable: true,
    configurable: true,
  },
  colorG: {
    get: function () {
      return this.color.g
    },
    set: function (v) {
      if (this.color.g == v) return
      this.color = new Color(this.color.r,v,this.color.b,this.color.a)
    },
    enumerable: true,
    configurable: true,
  },
  colorB: {
    get: function () {
      return this.color.b
    },
    set: function (v) {
      if (this.color.b == v) return
      this.color = new Color(this.color.r,this.color.g,v,this.color.a)
    },
    enumerable: true,
    configurable: true,
  },
  colorA: {
    get: function () {
      return this.color.a
    },
    set: function (v) {
      if (this.color.a == v) return
      this.color = new Color(this.color.r,this.color.g,this.color.b,v)
    },
    enumerable: true,
    configurable: true,
  },
})

Object.defineProperties(Node.prototype, {
  /** 2D节点的alpha[范围0~1]，实现原理，从当前节点获得UIRenderer对象，如果有就使用color.a，没有的话，就会获得UIOpacity，再修改UIOpacity的opacity */
  alpha: {
    get: function () {
      const uIRenderer: UIRenderer = this.getComponent(UIRenderer)
      if (uIRenderer) {
        return uIRenderer.colorA / 255
      }
      return null
    },
    set: function (v) {
      const uIRenderer: UIRenderer = this.getComponent(UIRenderer)
      if (uIRenderer) {
        if (uIRenderer.colorA == v * 255) return
        uIRenderer.colorA = v * 255
      }
    },
    enumerable: true,
    configurable: true,
  },

  positionX: {
    get: function () {
      return this.position.x
    },
    set: function (v) {
      if (this.position.x == v) return
      this.position.x = v
      this.setPosition(this.position)
    },
    enumerable: true,
    configurable: true,
  },
  positionY: {
    get: function () {
      return this.position.y
    },
    set: function (v) {
      if (this.position.y == v) return
      this.position.y = v
      this.setPosition(this.position)
    },
    enumerable: true,
    configurable: true,
  },
  positionZ: {
    get: function () {
      return this.position.z
    },
    set: function (v) {
      if (this.position.z == v) return
      this.position.z = v
      this.setPosition(this.position)
    },
    enumerable: true,
    configurable: true,
  },
  worldPositionX: {
    get: function () {
      return this.worldPosition.x
    },
    set: function (v) {
      if (this.worldPosition.x == v) return
      this.worldPosition.x = v
      this.setWorldPosition(this.worldPosition)
    },
    enumerable: true,
    configurable: true,
  },
  worldPositionY: {
    get: function () {
      return this.worldPosition.y
    },
    set: function (v) {
      if (this.worldPosition.y == v) return
      this.worldPosition.y = v
      this.setWorldPosition(this.worldPosition)
    },
    enumerable: true,
    configurable: true,
  },
  worldPositionZ: {
    get: function () {
      return this.worldPosition.z
    },
    set: function (v) {
      if (this.worldPosition.z == v) return
      this.worldPosition.z = v
      this.setWorldPosition(this.worldPosition)
    },
    enumerable: true,
    configurable: true,
  },
  eulerAngleX: {
    get: function () {
      return this.eulerAngles.x
    },
    set: function (v) {
      if (this.eulerAngles.x == v) return
      this.eulerAngles.x = v
      this.setRotationFromEuler(this.eulerAngles)
    },
    enumerable: true,
    configurable: true,
  },
  eulerAngleY: {
    get: function () {
      return this.eulerAngles.y
    },
    set: function (v) {
      if (this.eulerAngles.y == v) return
      this.eulerAngles.y = v
      this.setRotationFromEuler(this.eulerAngles)
    },
    enumerable: true,
    configurable: true,
  },
  eulerAngleZ: {
    get: function () {
      return this.eulerAngles.z
    },
    set: function (v) {
      if (this.eulerAngles.z == v) return
      this.eulerAngles.z = v
      this.setRotationFromEuler(this.eulerAngles)
    },
    enumerable: true,
    configurable: true,
  },
  scaleX: {
    get: function () {
      return this.scale.x
    },
    set: function (v) {
      if (this.scale.x == v) return
      this.scale.x = v
      this.setScale(this.scale)
    },
    enumerable: true,
    configurable: true,
  },
  scaleY: {
    get: function () {
      return this.scale.y
    },
    set: function (v) {
      if (this.scale.y == v) return
      this.scale.y = v
      this.setScale(this.scale)
    },
    enumerable: true,
    configurable: true,
  },
  scaleZ: {
    get: function () {
      return this.scale.z
    },
    set: function (v) {
      if (this.scale.z == v) return
      this.scale.z = v
      this.setScale(this.scale)
    },
    enumerable: true,
    configurable: true,
  },
  scaleXYZ: {
    get: function () {
      return this.scale.x
    },
    set: function (v) {
      this.setScale(v, v, v)
    },
    enumerable: true,
    configurable: true,
  },
  color:{
    get: function () {
      const uIRenderer: UIRenderer = this.getComponent(UIRenderer)
      if (uIRenderer) {
        return uIRenderer.color
      }
      return null
    },
    set: function (v:Color) {
      const uIRenderer: UIRenderer = this.getComponent(UIRenderer)
      if (uIRenderer) {
        if (uIRenderer.color == v) return
        uIRenderer.color = v
      }
    },
    enumerable: true,
    configurable: true,
  }
})
